/*
 * @lc app=leetcode.cn id=133 lang=javascript
 *
 * [133] 克隆图
 */

// @lc code=start
/**
 * // Definition for a Node.
 * function Node(val, neighbors) {
 *    this.val = val === undefined ? 0 : val;
 *    this.neighbors = neighbors === undefined ? [] : neighbors;
 * };
 */

/**
 * @param {Node} node
 * @return {Node}
 */

function getReault(node, map) {
  if(!node) return node;
  if(map.get(node)) return map.get(node);
  const newNode = new Node(node.val);
  map.set(node, newNode);
  
  for(let neighbor of node.neighbors) {
    newNode.neighbors.push(getReault(neighbor, map));
  }
  return newNode;
}
var cloneGraph = function(node) {
  let map = new Map();
  return getReault(node, map);
};
// @lc code=end

